package com.atgouwu.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atgouwu.mall.product.entity.BrandEntity;
import com.atgouwu.mall.product.service.BrandService;
import com.atgouwu.mall.common.utils.PageUtils;
import com.atgouwu.mall.common.utils.R;

/**
 * 品牌
 *
 * @author heran
 * @email sunlightcs@gmail.com
 * @date 2024-02-22 15:17:48
 */
@Api(tags = "品牌")
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

    //(首页下面)获取品牌
    @ApiOperation(value = "获取品牌", notes = "首页根据'排序字段和查询条数'获取品牌列表")
    @GetMapping("/api/brandList")
    public R brandList(@ApiParam(value = "排序字段", defaultValue = "sort", required = true) @RequestParam("sortField") String sortField, @ApiParam(value = "查询数量", defaultValue = "10", required = true) @RequestParam("limit") Integer limit) {
        List<BrandEntity> brandList = brandService.brandList(sortField, limit);
        return R.ok().put("data", brandList);
    }

}
